/************************************************************************************
**  
* @copyright (c) 2013-2100,  Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file	    duye_library.cpp
* @version     
* @brief      
* @author
* @date	    2014-06-20
* @note 
*
*  1. 2014-06-20 Created this file
* 
*/
#include <duye_logger.h>
#include <duye_library.h>

namespace duye {

static const int8* DUYE_LOG_PREFIX = "duye.system.library";

Library::Library() {}

Library::~Library() {}

void* Library::open(const int8* libPath, const int32 flags) {
	if (!libPath) {
		DUYE_ERROR("input libPath is NULL");
		return NULL;
	}
	
    void* handle = dlopen(libPath, flags);
	int8* error = dlerror();
	if (!handle || error) {
		DUYE_ERROR("dlopen error:%s", error);
		return NULL;
	}

	return handle;
}

bool Library::close(void* handle) {
	if (handle) {
		return dlclose(handle) == 0;
	}

	return false;
}

void* Library::getSym(void* handle, const int8* enter) {
	if (!handle) {
		DUYE_ERROR("library not open, handle is NULL");
		return NULL;
	}	

	if (!enter) {
		DUYE_ERROR("input enter is NULL");
		return NULL;
	}
	
	void* address = dlsym(handle, enter);
	int8* error = dlerror();
	if (!address || error) {
		DUYE_ERROR("dlsym error:%s", error);
		return NULL;		
	}

	return address;
}

}
